package com.sgb.kjwl.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.databinding.ActivityScanLoginBinding;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ScanLoginActivity extends AppActivity {

    private String data;
    private ActivityScanLoginBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static void start(Context context, String data) {
        Intent intent = new Intent();
        intent.putExtra("data", data);
        intent.setClass(context, ScanLoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init() {
        data = getIntent().getStringExtra("data");
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_scan_login);
        binding.include.ivBack.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(this);
        binding.btnPcLogin.setOnClickListener(this);
        binding.include.titleView.setText("扫码登录");
        getUserInfo();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_pc_login:
                getPcLoginStatus(data);
                break;
        }
    }

    private void getUserInfo() {
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                if (response.body() == null || response.body().getData() == null) return;
                binding.tvName.setText(response.body().getData().getUsername());
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
            }
        });
    }

    private void getPcLoginStatus(String data) {
        APIManager.getInstance().getUserAPI().loginToPC(data).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                if (response.body() == null) return;
                MToast.showToast(ScanLoginActivity.this, response.body().getMsg());
                MainActivity.start(ScanLoginActivity.this);
                finish();
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {

            }
        });
    }

}
